package com.bbs.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.bbs.bean.BbsBlock;
import com.bbs.bean.BbsTitleInfo;
import com.bbs.bean.BbsUserInfo;
import com.bbs.services.BbsBlockManager;
import com.bbs.services.BbsTitleInfoManager;
import com.bbs.services.BbsUserInfoManager;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class BbsUserInfoAction extends ActionSupport implements Preparable,ModelDriven<BbsUserInfo>{
	private static final String  LIST="/bbsUserInfo/bbsUserInfoList.jsp";
	private static final String  EDIT="/bbsUserInfo/bbsUserInfoEdit.jsp";
	private static final String  ADD="/bbsUserInfo/bbsUserInfoAdd.jsp";
	private static final String  SHOW="/bbsUserInfo/bbsUserInfoShow.jsp";
	private static final String  HOEM="/index.jsp";
	private static final String TITLEMAIN="/bbsTitleInfo/bbsTitleMainShow.jsp";
	private static final String LOGINMANAGER="/main.jsp";
	private static final String INDEXBOLCK="/index.jsp";
//	private static final String 
	
	private BbsUserInfoManager  bbsUserInfoManager;
	private BbsBlockManager bbsBlockManager;
	private BbsUserInfo  bbsUserInfo;
	private int id;
	private List<BbsUserInfo>  bbsUserInfoList;
	private List<BbsTitleInfo> bbsTitleInfoList;
	private List<BbsBlock> bbsBlockList;
	private BbsTitleInfoManager bbsTitleInfoManager;
	private String ids;
	private int page;
	private int maxcount ;
	private int maxpage;
	
	public List<BbsBlock> getBbsBlockList() {
		return bbsBlockList;
	}

	public void setBbsBlockList(List<BbsBlock> bbsBlockList) {
		this.bbsBlockList = bbsBlockList;
	}
	public void setBbsBlockManager(BbsBlockManager bbsBlockManager) {
		this.bbsBlockManager = bbsBlockManager;
	}

	public int getMaxpage() {
		return maxpage;
	}

	public void setMaxpage(int maxpage) {
		this.maxpage = maxpage;
	}

	public int getMaxcount() {
		return maxcount;
	}

	public void setMaxcount(int maxcount) {
		this.maxcount = maxcount;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public BbsUserInfo getBbsUserInfo() {
		return bbsUserInfo;
	}

	public void setBbsUserInfo(BbsUserInfo bbsUserInfo) {
		this.bbsUserInfo = bbsUserInfo;
	}
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	public List<BbsTitleInfo> getBbsTitleInfoList() {
		return bbsTitleInfoList;
	}

	public void setBbsTitleInfoList(List<BbsTitleInfo> bbsTitleInfoList) {
		this.bbsTitleInfoList = bbsTitleInfoList;
	}

	public void setBbsTitleInfoManager(BbsTitleInfoManager bbsTitleInfoManager) {
		this.bbsTitleInfoManager = bbsTitleInfoManager;
	}

	public List<BbsUserInfo> getBbsUserInfoList() {
		return bbsUserInfoList;
	}

	public void setBbsUserInfoList(List<BbsUserInfo> bbsUserInfoList) {
		this.bbsUserInfoList = bbsUserInfoList;
	}

	public void prepare() throws Exception {
	
		if(bbsUserInfo==null){
			bbsUserInfo=new BbsUserInfo();
		}
	}

	public void setBbsUserInfoManager(BbsUserInfoManager bbsUserInfoManager) {
		this.bbsUserInfoManager = bbsUserInfoManager;
	}

	public BbsUserInfo getModel() {
		// TODO Auto-generated method stub
		return bbsUserInfo;
	}
	
	public String list(){
		if(page==0){
			page=1;
		}
		int pagecount=page*20-20;
		bbsUserInfoList=bbsUserInfoManager.findByPage(pagecount);
		maxcount=bbsUserInfoManager.findCount();
		maxpage=maxcount/20+1;
		return LIST;
	}
	
	public String addShow(){

		return ADD;
	}
	
	public String edit(){
		bbsUserInfo=bbsUserInfoManager.findId(id);
		return EDIT;
	}
	
	public String updateInfo(){
		bbsUserInfoManager.update(bbsUserInfo);
		if(page==0){
			page=1;
		}
		int pagecount=page*20-20;
		bbsUserInfoList=bbsUserInfoManager.findByPage(pagecount);
		maxcount=bbsUserInfoManager.findCount();
		maxpage=maxcount/20+1;
		return LIST;
	}
	
	public String addInfo(){
		bbsUserInfoManager.save(bbsUserInfo);
		if(page==0){
			page=1;
		}
		int pagecount=page*20-20;
		bbsUserInfoList=bbsUserInfoManager.findByPage(pagecount);
		maxcount=bbsUserInfoManager.findCount();
		maxpage=maxcount/20+1;
		return LIST;
	}
	
	public String show(){
		bbsUserInfo=bbsUserInfoManager.findId(id);
		return SHOW;
	}
	
	public String delete(){
		bbsUserInfoManager.delete(ids);
		if(page==0){
			page=1;
		}
		int pagecount=page*20-20;
		bbsUserInfoList=bbsUserInfoManager.findByPage(pagecount);
		maxcount=bbsUserInfoManager.findCount();
		maxpage=maxcount/20+1;
		return LIST;
	}
	
	public String prePage(){
		page=page-1;
		int pagecount=page*20-20;
		bbsUserInfoList=bbsUserInfoManager.findByPage(pagecount);
		maxcount=bbsUserInfoManager.findCount();
		maxpage=maxcount/20+1;
		return LIST;
	}
	
	public String nextPage(){
		page=page+1;
		int pagecount=page*20-20;
		bbsUserInfoList=bbsUserInfoManager.findByPage(pagecount);
		maxcount=bbsUserInfoManager.findCount();
		maxpage=maxcount/20+1;
		
		return LIST;
	}
	//论坛主页登陆
	public String login(){
		bbsUserInfo=bbsUserInfoManager.getUniqued(bbsUserInfo,"email,password");
		ServletActionContext.getRequest().getSession().setAttribute("bbsUserInfo", bbsUserInfo);
		if(page==0){
			page=1;
		}
		int countPage=page*20-20;
		maxpage=bbsTitleInfoManager.getCountInfo()+1;
		bbsTitleInfoList=bbsTitleInfoManager.findAllByPage(countPage);
		return TITLEMAIN;
	}
	
	//登陆后台
	public String loginManager(){
		
		return null;
	}
	
	//论坛板块主页登陆
	public String blockLogin(){
		bbsUserInfo=bbsUserInfoManager.getUniqued(bbsUserInfo,"email,password");
		ServletActionContext.getRequest().getSession().setAttribute("bbsUserInfo", bbsUserInfo);
		bbsBlockList=bbsBlockManager.findAll();
		return INDEXBOLCK;
	}
	
	public void checkLogin()throws Exception{
		bbsUserInfo=bbsUserInfoManager.getUniqued(bbsUserInfo,"email,password");
		String next=null;
		if(bbsUserInfo==null){
			next="this";
		}else{
			next="success";
		}
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		out.print(next);
		out.flush();
		out.close();
	}
//检查是否登录
	public void isLogin()throws Exception{
		
		BbsUserInfo user=(BbsUserInfo)ServletActionContext.getRequest().getSession().getAttribute("bbsUserInfo");
		String next=null;
		if(user==null){
			next="null";
		}else{
			next="success";
		}
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		out.print(next);
		out.flush();
		out.close();
	}
	
	//
	public String logout(){
		ServletActionContext.getRequest().getSession().removeAttribute("bbsUserInfo");
		if(page==0){
			page=1;
		}
		int countPage=page*20-20;
		maxpage=bbsTitleInfoManager.getCountInfo()+1;
		bbsTitleInfoList=bbsTitleInfoManager.findAllByPage(countPage);
		return TITLEMAIN;
	}
	//论坛主页
	public String logoutBlock(){
		ServletActionContext.getRequest().getSession().removeAttribute("bbsUserInfo");
		bbsBlockList=bbsBlockManager.findAll();
		return INDEXBOLCK;
	}
	
	public String register(){
		bbsUserInfoManager.save(bbsUserInfo);
		return null;
	}
}
